﻿Public Class FileSelectionDialogs
  Public Shared Function GetAudioFile(ByVal db As AvianPlayDB, Optional ByVal filePath As String = "") As Playlists.AVPLPlaylistEntry
    Dim cancel As Boolean = False

    If db IsNot Nothing Then
      If filePath = "" Then
        Dim sf As New SelectFile(SelectFile.Mode.FilesAndFolders, Language.GetLanguageString("OpenAudioFile"))
        If sf.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
          filePath = sf.Path
        Else
          cancel = True
        End If
      End If

      If Not cancel Then
        If IO.File.Exists(filePath) Then
          Dim ple As New Playlists.AVPLPlaylistEntry

          Try
            Windows.Forms.Cursor.Current = Cursors.WaitCursor

            ' Fetch file data from DB
            Dim ft As AvianPlayDB.AudioFileTable = db.GetAudioFileDBData(filePath, True, False)

            ' No file data?  Then parse the file now
            If ft Is Nothing Then
              Windows.Forms.Cursor.Current = Cursors.WaitCursor

              ' Find the extension mapping for this file
              Dim aem As AudioExtensionMap = Settings.GetAudioExtensionMapForFile(filePath)

              ' Check if the extension is valid
              If aem IsNot Nothing Then
                ' Update the database with the information of the file
                Library.Monitor.UpdateAudioFileInDatabase(filePath, aem, db)

                ' Okay, now the DB is up-to-date, so get the file data now
                ft = db.GetAudioFileDBData(filePath, True, False)

                ' This is catching an error that hopefully will never happen
                If ft Is Nothing Then
                  Throw New Exception("Invalid FileTable Data")
                End If
              Else
                ' Unknown extension!  Just return nothing and warn the user...
                MessageBox.Show(Language.GetLanguageString("UnknownExtension"), "Avian Play", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
                Return Nothing
              End If
            End If

            ' Fill the playlist entry from the file table, so it's ready to return!
            AvianPlayDB.FillPlayListEntryFromFileTable(ft, ple)

            ' Return the entry
            Return ple
          Catch ex As Exception
            ErrorLog.WriteToErrorLog("SelectMusicFileIntoPLE :: Unable to open file :: " & filePath & " :: " & ex.ToString)
            MessageBox.Show(Language.GetLanguageString("UnableToOpenFile") & " " & ex.Message, "Avian Play", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
          Finally
            Windows.Forms.Cursor.Current = Cursors.Default
          End Try
        Else
          MessageBox.Show(Language.GetLanguageString("FileNotFound"), "Avian Play", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
        End If
      End If
    End If

    Return Nothing
  End Function

  Public Shared Function GetPlaylistFile() As Playlists.AVPLPlaylist
    Dim sf As New SelectFile(SelectFile.Mode.FilesAndFolders, Language.GetLanguageString("OpenPlaylistFile"))

    If sf.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
      If IO.File.Exists(sf.Path) Then
        Dim avpl As New Playlists.AVPLPlaylist

        Try
          Windows.Forms.Cursor.Current = Cursors.WaitCursor

          ' Find the extension mapping for this file
          Dim pem As PlaylistExtensionMap = Settings.GetPlaylistExtensionMapForFile(sf.Path)

          ' Check if the extension is valid
          If pem IsNot Nothing Then
            Select Case pem.Type
              Case PlaylistFileType.AVPL
                Return Playlists.PlaylistReader.ParseAVPLFile(sf.Path)
              Case PlaylistFileType.M3U
                Return Playlists.AVPLPlaylist.FromStandardPlaylist(Playlists.PlaylistReader.ParseM3UFile(sf.Path))
              Case PlaylistFileType.PLS
                Return Playlists.AVPLPlaylist.FromStandardPlaylist(Playlists.PlaylistReader.ParsePLSFile(sf.Path))
            End Select
          Else
            ' Unknown extension!  Just return nothing and warn the user...
            MessageBox.Show(Language.GetLanguageString("UnknownExtension"), "Avian Play", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
            Return Nothing
          End If
        Catch ex As Exception
          ErrorLog.WriteToErrorLog("SelectPlaylistFileIntoAVPL :: Unable to open file :: " & sf.Path & " :: " & ex.ToString)
          MessageBox.Show(Language.GetLanguageString("UnableToOpenFile") & " " & ex.Message, "Avian Play", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
        Finally
          Windows.Forms.Cursor.Current = Cursors.Default
        End Try
      Else
        MessageBox.Show(Language.GetLanguageString("FileNotFound"), "Avian Play", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
      End If
    End If

    Return Nothing
  End Function
End Class
